﻿using UnityEngine;
using System.Collections;

[RequireComponent(typeof(PlayerPhysics))]

public class PlayerController : MonoBehaviour {

	public float gravity = 20f;
	public float minSpeed = -50f; //max Geschwindigkeit Rückwärts
	public float maxSpeed = 100f;	//max vorwärts
	public float acceleration = 30f;	//beschleunigung
	public float currentSpeed = 0f;	//aktuelle Geschwindigkeit
	public Vector2 amountToMove;

	public float jumpHeight = 12;

	private PlayerPhysics playerPhysics;
	private Animator animator;

	// Use this for initialization
	void Start () 
	{
		playerPhysics = GetComponent<PlayerPhysics>();
		animator = GetComponent<Animator> ();
	}
	
	// Update is called once per frame
	void Update () 
	{


		if (playerPhysics.stopedMove) 
		{
			currentSpeed = 0;	
			
		}
		currentSpeed = incrementSpeed();

		if (playerPhysics.grounded) 	//jump
		{
			amountToMove.y = 0;

			if(Input.GetButtonDown("Jump"))
			{
				amountToMove.y = jumpHeight;
			}
		}
		animator.SetFloat ("Speed", Mathf.Abs(currentSpeed));

		amountToMove.x = currentSpeed;
		amountToMove.y -= gravity * Time.deltaTime;


		playerPhysics.movePlayer(amountToMove*Time.deltaTime);
	}

	public float incrementSpeed()
	{
		float speed = 0f;
		speed += Input.GetAxisRaw("Horizontal")*acceleration; //beschleunigung draufrechnen
		speed = Mathf.Clamp(speed, minSpeed, maxSpeed);	//wert clampen damit nicht min<wert>max
		return speed;
	}
}
